package com.xh.rufongwallpaper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xh.rufongwallpaper.entity.Preference;
import com.xh.rufongwallpaper.mapper.PreferenceMapper;
import com.xh.rufongwallpaper.service.PreferenceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * 用户偏好设置服务实现类
 *
 * @author XH
 */
@Service
@RequiredArgsConstructor
public class PreferenceServiceImpl implements PreferenceService {

    private final PreferenceMapper preferenceMapper;

    @Override
    public Optional<Preference> getPreferenceByDeviceId(Long deviceId) {
        return Optional.ofNullable(preferenceMapper.selectOne(
                new LambdaQueryWrapper<Preference>()
                        .eq(Preference::getDeviceId, deviceId)
                        .eq(Preference::getIsDeleted, false)
        ));
    }

    @Override
    @Transactional
    public Preference savePreference(Preference preference) {
        // 检查是否已存在
        Preference existingPreference = preferenceMapper.selectOne(
                new LambdaQueryWrapper<Preference>()
                        .eq(Preference::getDeviceId, preference.getDeviceId())
                        .eq(Preference::getIsDeleted, false)
        );
        
        if (existingPreference != null) {
            // 更新已有设置
            existingPreference.setDarkMode(preference.getDarkMode());
            existingPreference.setAutoSave(preference.getAutoSave());
            existingPreference.setWallpaperQuality(preference.getWallpaperQuality());
            existingPreference.setNotification(preference.getNotification());
            preferenceMapper.updateById(existingPreference);
            return existingPreference;
        }

        // 创建新设置
        preference.setIsDeleted(false);
        preferenceMapper.insert(preference);
        return preference;
    }

    @Override
    @Transactional
    public boolean updateDarkMode(Long deviceId, boolean darkMode) {
        Optional<Preference> preferenceOpt = getOrCreatePreference(deviceId);
        if (preferenceOpt.isPresent()) {
            return preferenceMapper.update(null,
                    new LambdaUpdateWrapper<Preference>()
                            .eq(Preference::getDeviceId, deviceId)
                            .eq(Preference::getIsDeleted, false)
                            .set(Preference::getDarkMode, darkMode)
            ) > 0;
        }
        return false;
    }

    @Override
    @Transactional
    public boolean updateAutoSave(Long deviceId, boolean autoSave) {
        Optional<Preference> preferenceOpt = getOrCreatePreference(deviceId);
        if (preferenceOpt.isPresent()) {
            return preferenceMapper.update(null,
                    new LambdaUpdateWrapper<Preference>()
                            .eq(Preference::getDeviceId, deviceId)
                            .eq(Preference::getIsDeleted, false)
                            .set(Preference::getAutoSave, autoSave)
            ) > 0;
        }
        return false;
    }

    @Override
    @Transactional
    public boolean updateWallpaperQuality(Long deviceId, String wallpaperQuality) {
        Optional<Preference> preferenceOpt = getOrCreatePreference(deviceId);
        if (preferenceOpt.isPresent()) {
            return preferenceMapper.update(null,
                    new LambdaUpdateWrapper<Preference>()
                            .eq(Preference::getDeviceId, deviceId)
                            .eq(Preference::getIsDeleted, false)
                            .set(Preference::getWallpaperQuality, wallpaperQuality)
            ) > 0;
        }
        return false;
    }

    @Override
    @Transactional
    public boolean updateNotification(Long deviceId, boolean notification) {
        Optional<Preference> preferenceOpt = getOrCreatePreference(deviceId);
        if (preferenceOpt.isPresent()) {
            return preferenceMapper.update(null,
                    new LambdaUpdateWrapper<Preference>()
                            .eq(Preference::getDeviceId, deviceId)
                            .eq(Preference::getIsDeleted, false)
                            .set(Preference::getNotification, notification)
            ) > 0;
        }
        return false;
    }

    /**
     * 获取设备偏好设置，如果不存在则创建默认设置
     *
     * @param deviceId 设备ID
     * @return 偏好设置
     */
    private Optional<Preference> getOrCreatePreference(Long deviceId) {
        Preference preference = preferenceMapper.selectOne(
                new LambdaQueryWrapper<Preference>()
                        .eq(Preference::getDeviceId, deviceId)
                        .eq(Preference::getIsDeleted, false)
        );
        
        if (preference == null) {
            // 创建默认设置
            preference = new Preference();
            preference.setDeviceId(deviceId);
            preference.setDarkMode(false);
            preference.setAutoSave(true);
            preference.setWallpaperQuality("high");
            preference.setNotification(true);
            preference.setIsDeleted(false);
            preferenceMapper.insert(preference);
        }
        
        return Optional.of(preference);
    }
} 